package com.hl.security;

import com.hl.domain.LiliShopMember;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.*;

//@Data
public class LoginUser implements UserDetails {
    private LiliShopMember liliShopMember;
    private Set<String> permissions;

    public Set<String> getPermissions() {
        return permissions;
    }

    public void setPermissions(Set<String> permissions) {
        this.permissions = permissions;
    }

    public LiliShopMember getLiliShopMember() {
        return liliShopMember;
    }

    public void setLiliShopMember(LiliShopMember liliShopMember) {
        this.liliShopMember = liliShopMember;
    }

    public LoginUser(LiliShopMember liliShopMember, Set<String> permissions) {
        this.liliShopMember = liliShopMember;
        this.permissions = permissions;
    }

    public LoginUser() {
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> list = new ArrayList<>();
        for (String permission : permissions) {
            list.add(new SimpleGrantedAuthority(permission));
        }
        return list;
    }

    @Override
    public String getPassword() {
        return liliShopMember.getPassword();
    }

    @Override
    public String getUsername() {
        return liliShopMember.getMemberName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
